package com.lxk.mall.pms.service.impl;

import com.lxk.mall.pms.domain.PmsProductStatistics;
import com.lxk.mall.pms.mapper.PmsProductStatisticsMapper;
import com.lxk.mall.pms.service.IPmsProductStatisticsService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.lxk.mall.pms.domain.bo.statics.PmsProductStatisticsBo;
import com.lxk.mall.pms.domain.vo.statics.PmsProductStatisticsVo;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品统计Service业务层处理
 *
 * @author Lion Li
 * @date 2024-06-06
 */
@RequiredArgsConstructor
@Service
public class PmsProductStatisticsServiceImpl implements IPmsProductStatisticsService {

    private final PmsProductStatisticsMapper baseMapper;

    /**
     * 查询商品统计
     *
     * @param id 主键
     * @return 商品统计
     */
    @Override
    public PmsProductStatisticsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询商品统计列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 商品统计分页列表
     */
    @Override
    public TableDataInfo<PmsProductStatisticsVo> queryPageList(PmsProductStatisticsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PmsProductStatistics> lqw = buildQueryWrapper(bo);
        Page<PmsProductStatisticsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的商品统计列表
     *
     * @param bo 查询条件
     * @return 商品统计列表
     */
    @Override
    public List<PmsProductStatisticsVo> queryList(PmsProductStatisticsBo bo) {
        LambdaQueryWrapper<PmsProductStatistics> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PmsProductStatistics> buildQueryWrapper(PmsProductStatisticsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PmsProductStatistics> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTime() != null, PmsProductStatistics::getTime, bo.getTime());
        lqw.eq(bo.getSpuId() != null, PmsProductStatistics::getSpuId, bo.getSpuId());
        lqw.eq(bo.getBrowseCount() != null, PmsProductStatistics::getBrowseCount, bo.getBrowseCount());
        lqw.eq(bo.getBrowseUserCount() != null, PmsProductStatistics::getBrowseUserCount, bo.getBrowseUserCount());
        lqw.eq(bo.getFavoriteCount() != null, PmsProductStatistics::getFavoriteCount, bo.getFavoriteCount());
        lqw.eq(bo.getCartCount() != null, PmsProductStatistics::getCartCount, bo.getCartCount());
        lqw.eq(bo.getOrderCount() != null, PmsProductStatistics::getOrderCount, bo.getOrderCount());
        lqw.eq(bo.getOrderPayCount() != null, PmsProductStatistics::getOrderPayCount, bo.getOrderPayCount());
        lqw.eq(bo.getOrderPayPrice() != null, PmsProductStatistics::getOrderPayPrice, bo.getOrderPayPrice());
        lqw.eq(bo.getAfterSaleCount() != null, PmsProductStatistics::getAfterSaleCount, bo.getAfterSaleCount());
        lqw.eq(bo.getAfterSaleRefundPrice() != null, PmsProductStatistics::getAfterSaleRefundPrice, bo.getAfterSaleRefundPrice());
        lqw.eq(bo.getBrowseConvertPercent() != null, PmsProductStatistics::getBrowseConvertPercent, bo.getBrowseConvertPercent());
        return lqw;
    }

    /**
     * 新增商品统计
     *
     * @param bo 商品统计
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(PmsProductStatisticsBo bo) {
        PmsProductStatistics add = MapstructUtils.convert(bo, PmsProductStatistics.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品统计
     *
     * @param bo 商品统计
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(PmsProductStatisticsBo bo) {
        PmsProductStatistics update = MapstructUtils.convert(bo, PmsProductStatistics.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PmsProductStatistics entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除商品统计信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
